import * as THREE from 'three';
import EventBus from '../../assets/bus';
import { loaderRoam } from './loaderRoam';

export function loaderModel(app) {
  app.rayModel = [];
  app.helperBox = {};
  const oaNames = ['地板', '墙', '天窗栅栏', '房顶'];
  const rayModelNames = ['地板', '墙', '沙发', '中间屏'];
  return new Promise((resolve) => {
    const urls = [
      {
        type: 'rgbe',
        url: 'texture/royal_esplanade_1k.hdr',
        onLoad: (texture) => {
          texture.mapping = THREE.EquirectangularReflectionMapping;
          app.envMap = texture;
        }
      },
      {
        type: 'gltf',
        url: 'model/model.glb',
        onLoad: (object) => {
          console.log(object);

          app.model = object.scene;
        }
      }
    ];

    let urlsLength = urls.length;
    app.iterateLoad(
      urls,
      (xhr) => {
        let proportion = parseInt((xhr.loaded / xhr.total) * 100);
        if (proportion === 100) {
          EventBus.$emit('changeLoaidng', parseInt(100 / urlsLength));
          urlsLength--;
          if (urlsLength <= 1) {
            EventBus.$emit('changeScene', true);
          }
        }
      },
      () => {
        app.model.traverse((obj) => {
          if (obj.isGroup) {
            if (rayModelNames.includes(obj.name)) {
              obj.children.forEach((item) => {
                app.rayModel.push(item);
              });
            }
          }

          if (obj.isMesh) {
            // if (obj.name === '房顶') {
            //   obj.visible = false;
            // }

            if (oaNames.includes(obj.name)) {
              const basicMaterial = new THREE.MeshBasicMaterial();
              basicMaterial.map = obj.material.map;
              basicMaterial.color = obj.material.color;
              obj.material = basicMaterial;
            } else {
              obj.material.envMap = app.envMap;
            }

            if (rayModelNames.includes(obj.name)) {
              app.rayModel.push(obj);
            }

            if (obj.name.indexOf('pic') > -1) {
              app.rayModel.push(obj);
            }

            if (obj.name.indexOf('helperBox') > -1) {
              obj.geometry.computeBoundingBox();
              const box = new THREE.Box3().setFromObject(obj);

              const position = app.getModelWorldPostion(obj);

              const childrenPosition = app.getModelWorldPostion(obj.children[0]);

              const subVec3 = childrenPosition
                .clone()
                .sub(position)
                .normalize();

              let x = 0;
              let z = 0;

              if (Math.abs(subVec3.x) > Math.abs(subVec3.z)) {
                x = subVec3.x > 0 ? 0.1 : -0.1;
              } else {
                z = subVec3.z > 0 ? 0.1 : -0.1;
              }

              const number = 100;
              position.x += -number * x;
              position.z += -number * z;

              app.helperBox[obj.name] = {
                model: obj,
                box,
                position: [position.x, childrenPosition.y, position.z],
                controls: [position.x + x, childrenPosition.y, position.z + z]
              };

              // const helper = new THREE.Box3Helper(box, 0xff0000);
              // app.scene.add(helper);
            }
          }
        });
        app.scene.add(app.model);
        loaderRoam(app);
        resolve();
      }
    );
  });
}
